/*
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class jdbc_cleanup_timer_case0001{
    private ExecutorService service = Executors.newFixedThreadPool(10);
    private ExecutorService service1 = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ClassNotFoundException,SQLException{
        String url = "jdbc:postgresql://" + args[0] + "/" + args[1];
        String user = args[2];
        String password = args[3];
        jdbc_cleanup_timer_case0001 cleanupTimer = new jdbc_cleanup_timer_case0001();
        for (Connection conn:cleanupTimer.getConns(url,user,password)){
            cleanupTimer.service.execute(() -> cleanupTimer.createStatement(cleanupTimer,conn));
        }
        cleanupTimer.service.shutdown();
    }

    private  void createStatement(jdbc_cleanup_timer_case0001 cleanupTimer,Connection conn){
        for (int i=0;i<3;i++){
            int finall = i;
            cleanupTimer.service1.execute(() -> cleanupTimer.execute(conn, conn+":i-"+finall));
        }
        cleanupTimer.service1.shutdown();
    }

    private void execute(Connection connection, String name){
        try {
            Statement statement = connection.createStatement();
            statement.setQueryTimeout(15);
            ResultSet resultSet;
            resultSet = statement.executeQuery("select * from t_jdbc_cleanup_timer_01;");
            while (resultSet.next()){
                final Object res_objectest_point01 = resultSet.getObject("i");
                System.out.println("res_objectest_point01:" + res_objectest_point01);
            }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
    private List<Connection> getConns(String url,String user,String password) throws SQLException,ClassNotFoundException{
        List<Connection> connectionList = new ArrayList<>();
        for (int i=0;i<5;i++){
            Connection conn = DriverManager.getConnection(url,user,password);
            connectionList.add(conn);
        }
        return connectionList;
    }
}


